package com.vote.www.controller;

import com.vote.www.core.page.Pagination;
import com.vote.www.domain.Firsttrip;
import com.vote.www.service.FirsttripService;
import com.vote.www.utils.ConvertObj;
import com.vote.www.utils.UUIDUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *   代言人出行首位冠军
 * Created by 汤伟杰 on 2017/5/23.
 */
@Controller
@RequestMapping("/firsttrip")
public class FirsttripController extends BaseController{
    @Autowired
    private FirsttripService firsttripService;


    @RequestMapping("/list.do")
    public String list(Integer pageNo, Integer pageSize, Firsttrip firsttrip, HttpServletRequest req, HttpServletResponse resp,
                       ModelMap model){
        setMsg(firsttrip,req,model);
        if (pageNo == null) {
            pageNo = 1;
        }
        if (pageSize == null) {
            pageSize = 0;
        }
        try {
            Map<String, Object> params = ConvertObj.toMap(firsttrip);
            Pagination pagination = firsttripService.list(pageNo.intValue(), pageSize.intValue(), params, model);
            model.addAttribute("pagination", pagination);
        } catch (Exception e) {
            e.printStackTrace();
            model.addAttribute("failed","获取代言人出行首位冠军列表失败!"+e.getMessage());
        }
        model.addAttribute("entity", firsttrip);
        return "firsttrip/list";
    }

    @RequestMapping("/save.do")
    public String save(Firsttrip firsttrip,
                       HttpServletRequest req, HttpServletResponse resp,
                       ModelMap model){
        try {

            if(StringUtils.isNotBlank(firsttrip.getFirsttripId())){
                firsttripService.update(firsttrip);


            }else{
                firsttrip.setFirsttripId(UUIDUtils.getUUID());
                firsttripService.insert(firsttrip);
            }
            model.addAttribute("success", "保存代言人出行首位冠军成功");
        } catch (Exception e) {
            e.printStackTrace();
            model.addAttribute("failed", "保存代言人出行首位冠军失败!详情："+e.getMessage());
        }
        return "redirect:list.do";
    }



    @RequestMapping("/detail.do")
    public String detail(Firsttrip firsttrip, HttpServletRequest req, HttpServletResponse resp,
                         ModelMap model){
        setMsg(firsttrip,req,model);
        model.addAttribute("entity", new HashMap<String,Object>());
        model.addAttribute("userapprove", new HashMap<String,Object>());
        try {
            if(StringUtils.isNotBlank(firsttrip.getFirsttripId())){
                firsttripService.queryDetailById(firsttrip.getFirsttripId(),model);
            }else{
                model.addAttribute("failed", "不存在该数据！");
            }

        } catch (Exception e) {
            e.printStackTrace();
            model.addAttribute("failed", "查询代言人出行首位冠军出错!详情："+e.getMessage());
        }
        return "firsttrip/detail";
    }
    @RequestMapping("/add.do")
    public String add(Firsttrip firsttrip, HttpServletRequest req, HttpServletResponse resp,
                      ModelMap model){
        setMsg(firsttrip,req,model);
        try {
            getUserApproveList(req,resp,model);
            if(StringUtils.isNotBlank(firsttrip.getFirsttripId())){
                firsttrip = firsttripService.queryById(firsttrip.getFirsttripId());
                model.addAttribute("entity", firsttrip);
            }

        } catch (Exception e) {
            e.printStackTrace();
            model.addAttribute("failed", e.getMessage());
        }
        return "firsttrip/add";
    }
    @RequestMapping("/delete.do")
    public String delete(Firsttrip firsttrip, HttpServletRequest req, HttpServletResponse resp,
                      ModelMap model){
        try {
            firsttripService.deleteById(firsttrip.getFirsttripId());
        } catch (Exception e) {
            e.printStackTrace();
            model.addAttribute("failed", e.getMessage());
        }
        return "redirect:list.do";
    }

    private void setMsg(Firsttrip firsttrip,HttpServletRequest req, ModelMap model) {
        model.addAttribute("failed", req.getParameter("failed"));
        model.addAttribute("success", req.getParameter("success"));
        model.addAttribute("entity", firsttrip);
    }

    public void getUserApproveList(HttpServletRequest req, HttpServletResponse resp,
                       ModelMap model){
        List<Map<String,Object>>  listmap = firsttripService.getUserApproveList();
        model.addAttribute("listUserApprove", listmap);
    }
}
